<template>
  <div>
    <el-form
      :inline="true"
      :model="formInline"
      class="demo-form-inline"
      @submit.native.prevent
      v-if="hasAuth('sys:department:find') && hasAuth('sys:department:add')"
    >
      <el-form-item v-if="hasAuth('sys:department:find')">
        <el-input
          v-model="formInline.departmentName"
          placeholder="请输入部门名称"
          @keyup.enter.native="findByDepartmentName"
        ></el-input>
      </el-form-item>
      <el-form-item v-if="hasAuth('sys:department:find')">
        <el-button type="primary" round @click="findByDepartmentName"
          >查询</el-button
        >
      </el-form-item>
      <el-form-item v-if="hasAuth('sys:department:add')">
        <el-button type="success" round @click="addDepartmentBtn"
          >新增</el-button
        >
      </el-form-item>
      <el-form-item v-if="hasAuth('sys:department:del')">
        <el-popconfirm title="确定删除吗？" @confirm="delDepartmentBtn(null)">
          <el-button
            type="danger"
            round
            slot="reference"
            :disabled="batchDelStatus"
            >批量删除</el-button
          >
        </el-popconfirm>
      </el-form-item>
    </el-form>
    <el-table
      :data="departmentList"
      style="width: 100%"
      :max-height="tableHeight"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection"> </el-table-column>
      <el-table-column prop="name" label="部门名称"> </el-table-column>
      <el-table-column prop="descriptors" label="部门介绍"> </el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间"
        v-if="hasAuth('sys:department:edit')"
      >
      </el-table-column>
      <el-table-column
        prop="updateTime"
        label="更新时间"
        v-if="hasAuth('sys:department:edit')"
      >
      </el-table-column>
      <el-table-column
        label="操作"
        v-if="hasAuth('sys:department:edit') || hasAuth('sys:department:del')"
        width="130px"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="editDepartmentBtn(scope.row.id)"
            v-if="hasAuth('sys:department:edit')"
            >编辑</el-button
          >
          <el-divider
            direction="vertical"
            v-if="
              hasAuth('sys:department:edit') && hasAuth('sys:department:del')
            "
          ></el-divider>
          <el-popconfirm
            title="确定删除吗？"
            @confirm="delDepartmentBtn(scope.row.id)"
          >
            <el-button
              slot="reference"
              type="text"
              v-if="hasAuth('sys:department:del')"
              >删除</el-button
            >
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      layout="total, sizes, prev, pager, next, jumper"
      :current-page="current"
      :page-sizes="[10, 15, 20, 50]"
      :page-size="size"
      :total="total"
    >
    </el-pagination>
    <!-- 新增或编辑部门dialog -->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogFormVisible"
      width="600px"
      :close-on-click-modal="false"
      :before-close="handleClose"
    >
      <el-form
        :model="editForm"
        :rules="rules"
        ref="editForm"
        label-position="left"
        label-width="100px"
        class="demo-editForm"
      >
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>
        <el-form-item label="部门介绍" prop="descriptors">
          <el-input
            type="textarea"
            :rows="4"
            v-model="editForm.descriptors"
          ></el-input>
        </el-form-item>
        <el-form-item class="btnGroup">
          <el-button
            type="primary"
            @click="submitForm('editForm')"
            style="float: right; margin-left: 15px"
            >确认</el-button
          >
          <el-button @click="handleClose" style="float: right">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "Department",
  data() {
    return {
      currentDepartmentId: 0,
      currentDepartmentManager: "",
      current: 1,
      size: 10,
      total: 0,      
      tableHeight: 600,
      batchDelStatus: true,
      dialogFormVisible: false,
      dialogTitle: "",
      departmentList: [],
      multipleSelection: [],
      formInline: {
        departmentName: "",
      },
      editForm: { id: -1 },
      rules: {
        name: [
          {
            type: "string",
            required: true,
            message: "部门名称必填",
            trigger: "blur",
          },
          {
            min: 3,
            max: 12,
            message: "长度在 3 到 12 个字符",
            trigger: "blur",
          },
        ],
        descriptors: [
          { required: true, message: "请输入部门介绍", trigger: "blur" },
          {
            max: 50,
            message: "长度最长为50个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    findByDepartmentName() {
      this.current = 1;
      // console.log(this.formInline.departmentName);
      this.getDepartmentList();
    },
    editDepartmentBtn(id) {
      this.dialogTitle = "编辑部门";
      this.getInfoById(id);
      this.dialogFormVisible = true;
    },
    handleSizeChange(val) {
      this.size = val;
      this.getDepartmentList();
    },
    handleCurrentChange(val) {
      this.current = val;
      this.getDepartmentList();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      this.batchDelStatus = val.length == 0;
      // console.log(this.multipleSelection);
    },
    dialogClose() {
      this.editForm = {
        id: -1,
      };
      this.showDepartmentInfoDialog = false;
    },
    handleClose() {
      this.$refs["editForm"].resetFields();
      this.editForm = { id: -1 };
      this.dialogFormVisible = false;
    },
    addDepartmentBtn() {
      this.dialogTitle = "新增部门";
      this.dialogFormVisible = true;
    },
    delDepartmentBtn(id) {
      var ids = [];
      if (id) {
        ids.push(id);
      } else {
        this.multipleSelection.forEach((row) => {
          ids.push(row.id);
        });
      }
      // console.log("ids:" + ids);
      this.$axios.post("/department/remove", ids).then((res) => {
        this.$message({
          showClose: true,
          type: "success",
          message: "删除成功!",
          onClose: () => {
            this.getDepartmentList();
          },
        });
      });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.editForm.id < 0) {
            this.$axios.post("/department/add", this.editForm).then((res) => {
              this.$message({
                showClose: true,
                type: "success",
                message: res.data.message,
                onClose: () => {
                  this.getDepartmentList();
                },
              });
            });
          } else {
            this.$axios
              .post("/department/update", this.editForm)
              .then((res) => {
                this.$message({
                  showClose: true,
                  type: "success",
                  message: res.data.message,
                  onClose: () => {
                    this.getDepartmentList();
                  },
                });
              });
          }
          this.handleClose();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    getInfoById(id) {
      this.$axios
        .get("/department/info", {
          params: {
            id: id
          },
        })
        .then((res) => {
          this.editForm = res.data.data;
        });
    },
    getDepartmentList() {
      this.$axios
        .get("/department/page", {
          params: {
            departmentName: this.formInline.departmentName,
            current: this.current,
            size: this.size,
          },
        })
        .then((res) => {
          this.departmentList = res.data.data.records;
          this.total = res.data.data.total;
        });
    },
  },
  created() {
    this.getDepartmentList();
  },
  // mounted() {
  //   this.timer = window.setInterval(() => {
  //     setTimeout(() => {
  //       this.getDepartmentList();
  //     }, 0);
  //   }, 3000);
  // },
  // destroyed() {
  //   window.clearInterval(this.timer);
  // },
};
</script>
<style scoped>
.text {
  padding: 0px 15px;
  margin: 0px;
}
.demo-form-inline {
  float: right;
}
.demo-editForm {
  margin: 0px 30px;
}
.btnGroup {
  margin-top: 50px;
}
</style>
